import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_business_core/business_customer/ful_components/business_customer_details_tag_button.dart';
import 'package:x3_business_core/business_customer/page/business_customer_details_tag_edit.dart';
import 'package:x3_component_core/x3_component_core.dart';

class BusinessCustomerDetailsTags extends StatefulWidget {
  final String vipId;

  const BusinessCustomerDetailsTags({Key key, @required this.vipId}) : super(key: key);

  @override
  _BusinessCustomerDetailsTagsState createState() => _BusinessCustomerDetailsTagsState();
}

class _BusinessCustomerDetailsTagsState extends State<BusinessCustomerDetailsTags> {
  String get _vipId => widget.vipId;

  List _tabList = [];

  BaseFutureController _baseFutureController = BaseFutureController();

  @override
  Widget build(BuildContext context) {
    return BaseCardComponent(
      child: Column(
        children: [
          _buildEditButton(),
          _buildTagList(),
        ],
      ),
    );
  }

  Widget _buildEditButton() {
    return InkWell(
      child: Container(
        padding: EdgeInsets.only(left: 4, top: 4, bottom: 4),
        child: Row(
          children: [
            Text('会员标签', style: Theme.of(context).textTheme.bodyText2.copyWith(fontWeight: FontWeight.bold)),
            Expanded(child: SizedBox()),
            Text('添加标签', style: Theme.of(context).textTheme.subtitle2.copyWith(color: Color(0xFFCCCCCC))),
            BaseComponentUtils.buildListViewRightIcon(context),
          ],
        ),
      ),
      onTap: () {
        BaseRouterUtil().pushNamed(BusinessCustomerDetailsTagEdit.sName, arguments: {'vipId': _vipId}).then((value) {
          _baseFutureController.refresh();
        });
      },
    );
  }

  Widget _buildTagList() {
    return BaseScreenRefreshComponent(
      controller: _baseFutureController,
      request: BaseScreenRequestModel('/vip/customer/tag/query/viptag/{vipId}', params: {'vipId': _vipId}, method: BaseHttpMethodEnum.GET),
      builder: (result) {
        if (result.success != true) return SizedBox();
        _tabList = result.data as List;
        if (_tabList.length == 0) return SizedBox();
        return Container(
          margin: EdgeInsets.symmetric(vertical: 4),
          child: BaseWrapExpandDown(limit: 10, data: _tabList, builder: _buildTag),
        );
      },
    );
  }

  Widget _buildTag(dynamic item) {
    String value = item['value'] ?? '';
    String key = item['key'] ?? '';
    return BusinessCustomerDetailsTagButton(label: value, value: key);
  }
}
